# -*- coding: utf-8 -*-
# @Time     : 2022/5/5 19:18
# @Author   : JustFly
# @File     : t.py.py
# @Software : PyCharm

import requests
import json
import time
import pymysql
import traceback
import sys
from selenium import webdriver


def get_area_data(adict, t_count):
    """
    相应格式
    :param area: 地区名
    :param t_count:
    :return:
    """
    print(adict['name'], adict['today'], adict['total'])
    t_count = t_count + 1
    print("\t" * t_count + "\n")
    try:
        get_area_data(adict['children'][0], t_count)
    except:
        print("children is None.")

def get_tencent_data():
    """
    :return: list全国汇总数据/日期为主键每日更新 list当日详细数据
    """

    url = "https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=statisGradeCityDetail,diseaseh5Shelf"
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'
    }

    r = requests.get(url, headers)
    rdict = r.json()

    # for it in rdict.items():
    #     print(it)

    data_all = rdict['data']['diseaseh5Shelf']['areaTree'][0]

    #t_count = 0
    #get_area_data(china, t_count)

    # print(res['data'])
    # data_all = json.loads(res['data'])
    # print(data_all)

    just_a_time = data_all['children'][0]['total']

    update_time = just_a_time['mtime']
    chinaTotal = data_all['total']

    # 全国汇总历史数据  suspect 的值由 当前疑似 改为 当前确诊

    ds = "今天的日期"  # 构造数据库字段
    confirm = chinaTotal['confirm']
    suspect = chinaTotal['nowConfirm']
    heal = chinaTotal['heal']
    dead = chinaTotal['dead']

    confirm_add = data_all['today']['confirm']
    heal_add = data_all['children'][0]['total']['heal']
    dead_add = data_all['children'][0]['total']['dead']

    history = [ds, confirm, confirm_add, suspect, suspect_add, heal, heal_add, dead, dead_add]

    # 全国各省市当日详情数据
    details = []

    data_province = data_all['areaTree'][0]['children']  # 中国各省

    for pro_infos in data_province:

        province = pro_infos['name']  # 省

        for city_infos in pro_infos['children']:
            city = city_infos['name']  # 市

            confirm = city_infos['total']['confirm']
            confirm_add = city_infos['today']['confirm']
            heal = city_infos['today']['confirm']
            dead = city_infos['total']['dead']
            details.append([update_time, province, city, confirm, confirm_add, heal, dead])

    return history, details





'''

{
'name': '河南',
'date': '2022/05/05',
'today': {'wzz_add': 26, 'confirm': 18, 'confirmCuts': 0, 'isUpdated': True, 'tip': ''},
'total': {'dead': 22, 'showHeal': True, 'provinceLocalConfirm': 2728, 'adcode': '410000', 'continueDayZeroLocalConfirmAdd': 0, 'continueDayZeroLocalConfirm': 0, 'nowConfirm': 59, 'showRate': False, 'highRiskAreaNum': 0, 'mtime': '2022-05-05 11:01:36', 'confirm': 2939, 'heal': 2858, 'wzz': 154, 'mediumRiskAreaNum': 2},
'children':


{
'ret': 0,
'info': '',
'data': {'diseaseh5Shelf': {'isShowAdd': True, 'showAddSwitch': {'nowConfirm': True, 'noInfect': True, 'localConfirm': True, 'heal': True, 'confirm': True, 'suspect': True, 'dead': True, 'nowSevere': True, 'im
portedCase': True, 'localinfeciton': True, 'all': True}, 

'areaTree': [
{'name': '中国',
 'today': {'confirm': 58941, 'isUpdated': True},
'total': {'mtime': '', 'nowConfirm': 492218, 'heal': 274475, 'wzz': 0, 'adcode': '', 'highRiskAreaNum': 0, 'confirm': 782043, 'dead': 15350, 'showHeal': True, 'provinceLocalConfirm': 0, 'continueDayZeroLocalConfirmAdd': 0, 'showRate': False, 'mediumRiskAreaNum': 0, 'continueDayZeroLocalConfirm': 0}, 'children':
'''





get_tencent_data()